﻿Imports System.Drawing
Imports System.Windows.Forms
Imports GameEx

Public Class DebugConsole

    Private _WindowTitle As String = "Debug Window"
    Private _timestamp As Boolean = False

    Property WindowTitle() As String
        Get
            Return _WindowTitle
        End Get
        Set(value As String)
            _WindowTitle = value
        End Set
    End Property

    Property TimeStamp() As Boolean
        Get
            Return _timestamp
        End Get
        Set(value As Boolean)
            _timestamp = value
        End Set
    End Property

    Private _textcolor As Color = Color.LimeGreen
    Public Property TextColor() As Color
        Get
            Return _textcolor
        End Get
        Set(ByVal value As Color)
            _textcolor = value
        End Set
    End Property


    Private Sub DebugConsole_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

        If e.CloseReason = CloseReason.UserClosing Then
            e.Cancel = True
            'Me.Hide()
        End If
        My.Settings.ConPos = Me.Location
        My.Settings.ConSize = Me.Size
        My.Settings.Save()
        Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Debug Console CLOSED.")
    End Sub

    Public Sub DebugConsole_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.Text = _WindowTitle

        'Me.Size = My.Settings.ConSize
        'Me.Location = My.Settings.ConPos
        ''Check Starting window pos
        'If Not (IsOnScreen(Me)) Then
        '    Me.Location = New Point(10, 10)
        'End If

        'If Me.Size.Height < 100 Or Me.Size.Width < 100 Then
        '    Me.Size = New Size(600, 400)
        'End If

        DBOut.AppendText("Start of Log: " & DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & vbCr & vbCr)

    End Sub

    Public Function IsOnScreen(ByVal form As Form) As Boolean
        'Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Check Form on Screen....")
        Dim screens() As Screen = Screen.AllScreens
        For Each scrn As Screen In screens
            ' Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Screen X,Y: " & scrn.Bounds.X & ", " & scrn.Bounds.Y)
            ' Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Screen W,H: " & scrn.Bounds.Width & ", " & scrn.Bounds.Height)
            'Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: ------")
            Dim formRectangle As Rectangle = New Rectangle(form.Left, form.Top, form.Width, form.Height)
            If scrn.WorkingArea.Contains(formRectangle) Then
                ' Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Form in bounds of this screen. Return True.")
                Return True
            End If
        Next
        ' Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Form not in the bounds of any screen. Return False.")
        Return False
    End Function

    Public Sub echo(MainText As String, Optional ErrorFlag As Boolean = False, Optional Heading As String = "")
        Dim Out As String = ""
        If _timestamp = True Then Out += DateTime.Now.ToString("HH:mm:ss") & " :: "
        If ErrorFlag = True Then Out += "ERROR: "
        If Heading <> "" Then Out += Heading.ToUpper & ": "
        Out += MainText & vbCr
        DBOut.SelectionColor = _textcolor
        DBOut.AppendText(Out)
        'DBOut.Refresh()
        DBOut.SelectionStart = DBOut.Text.Length
        DBOut.ScrollToCaret()
    End Sub

    Public Sub Shazaam()
        Me.BringToFront()
        Me.Show()
        Me.Focus()
        Me.Refresh()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        FontDialog1.Font = Me.Font
        FontDialog1.ShowDialog()
        Me.Font = FontDialog1.Font

    End Sub


    Private Sub DebugConsole_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
        My.Settings.ConPos = Me.Location
        My.Settings.ConSize = Me.Size
        My.Settings.Save()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        DBOut.AppendText("================================================================")
        DBOut.AppendText(vbCr)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ' Get the text from your rich text box
        'Dim textContents As String = DBOut.Text

        ' Copy the text to the clipboard

        DBOut.SelectAll()
        DBOut.Copy()


        'Clipboard.SetText(textContents)
    End Sub

    Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem.Click
        'CopyFromRichTextBox(DBOut)
        DBOut.Copy()

    End Sub


    Public Sub CopyFromRichTextBox(ByVal rtb As RichTextBox, _
            Optional ByVal availableAfterEnd As Boolean = False)

        Dim data As New DataObject()

        Dim rtfText, plainText As String
        If DBOut.SelectionLength > 0 Then
            rtfText = DBOut.SelectedRtf
            plainText = DBOut.SelectedText
        Else
            rtfText = DBOut.Rtf
            plainText = DBOut.Text
        End If
        ' do the copy only if there is something to be copied
        If rtfText.Length > 0 Then data.SetData(DataFormats.Rtf, rtfText)
        If plainText.Length > 0 Then data.SetData(DataFormats.Text, plainText)

        ' finally copy into the clipboard
        Clipboard.SetDataObject(data, availableAfterEnd)
    End Sub

    Private Sub DebugConsole_MenuComplete(sender As Object, e As EventArgs) Handles Me.Shown

        Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Debug Console shown.")
        Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Size: " & My.Settings.ConSize.ToString)
        Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Pos: " & My.Settings.ConPos.ToString)

        Me.Size = My.Settings.ConSize
        Me.Location = My.Settings.ConPos

        'Me.Activate()
        'Check Starting window pos
        If Not (IsOnScreen(Me)) Then
            Plugin.GEmvoy.WriteGVEntry(">> DebugConsole: Console not in bounds of any screen. Default to 10,10.")
            Me.Location = New Point(10, 10)
        End If

        If Me.Size.Height < 100 Or Me.Size.Width < 100 Then
            Me.Size = New Size(600, 400)
        End If
    End Sub



End Class


